<template>
  <el-dialog v-if="visible" :title="info.title" :visible.sync="visible" append-to-body center width="760px" :before-close="handleCancel">
    <el-form ref="formDate" label-suffix=":" label-width="120px" :rules="rules" :model="form">
      <el-row>
        <el-col :span="12">
          <el-form-item label="公司名称" prop="company">
            <el-select v-model="form.company" size="small" style="width: 100%">
              <el-option
                v-for="item in companys"
                :key="item"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="申请金额" prop="money">
            <el-input v-model="form.money" size="small" clearable style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="项目名称" prop="pro">
            <ProSelectObject v-model="form.pro" :id="form.pro_id" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item style="height: 37px; overflow: hidden" label="金额大写">
            <span v-if="form.word !== ''">{{ form.word }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="申请事由" prop="reason">
            <el-input v-model="form.reason" size="small" clearable style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="使用日期" prop="use_time">
            <el-date-picker
              v-model="form.use_time"
              type="date"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              size="small"
              clearable
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="归还日期" prop="return_time">
            <el-date-picker
              v-model="form.return_time"
              type="date"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              size="small"
              clearable
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
        <!-- <el-col :span="24">
          <el-form-item label="附件上传" prop="files">
            <Upload v-model="form.files" />
          </el-form-item>
        </el-col> -->
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" :rows="4" size="small" clearable style="width: 100%" />
          </el-form-item>
        </el-col>
        <!-- <el-col class="mt10" :span="24">
          <el-form-item label="抄送" prop="ccs">
            <VerifyCC v-model="form.ccs" :ccs="ccs" />
          </el-form-item>
        </el-col> -->
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="fr mt10">
            <el-button size="small" @click="handleCancel()">取 消</el-button>
            <el-button type="primary" size="small" @click="handleBeforeTo()">提 交</el-button>
          </div>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import ProSelectObject from '@/myComponents/ProSelectObject'
// import VerifyCC from '@/myComponents/VerifyCC'
import Upload from '@/myComponents/Upload'
import svf from '../../util/svf'
import { upMoney } from '@/api/finance/finance'

export default {
  components: { ProSelectObject, Upload },
  extends: svf,
  data() {
    return {
      approve_key: 'PROCESS_FINANCE_TEMPORARY',
      rules: {
        company: [{ required: true, trigger: 'blur', message: '公司名称不可为空' }],
        pro: [{ required: true, trigger: 'blur', message: '项目名称不可为空' }],
        reason: [{ required: true, trigger: 'blur', message: '申请事由不可为空' }],
        money: [{ required: true, trigger: 'blur', message: '申请金额不可为空' }],
        use_time: [{ required: true, trigger: 'blur', message: '使用日期不可为空' }],
        return_time: [{ required: true, trigger: 'blur', message: '归还日期不可为空' }]
        // files: [{ required: true, trigger: 'blur', message: '附件不可为空' }]
      }
    }
  },
  watch: {
    visible(val) {
      if (val) {
        if(this.info.type && this.info.type === 'again'){
          this.initApply(this.info.edit)
        }else{
          this.form = this.info.edit
        }
        this.$set(this.form, 'pro', {})
        // this.queryCcs()
      }
    },
    'form.money': {
      handler(n, o) {
        if (this.form.money && this.form.money !== '') {
          this.upMoney()
        }
      },
      deep: true
    }
  },
  mounted() {
  },
  methods: {
    upMoney() {
      upMoney({ money: this.form.money }).then(response => {
        this.form.word = response.word
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    handleBeforeTo() {
      this.validate().then(() => {
        const e = {}
        e.files = this.form.files
        e.approve_key = this.approve_key
        e.des = '公司名称：' + this.form.company + '    备注：' + this.form.remark
        e.ccs = this.form.ccs
        e.forms = JSON.stringify(this.createForms([
          ['PO单号', this.form.pro.key ? ('PO' + this.form.pro.key) : '', '', ''],
          ['项目名称', this.form.pro.value, this.form.pro.key, 'pro_id'],
          ['公司名称', this.form.company, this.form.company, 'company'],
          ['申请金额', this.form.money, this.form.money, 'money'],
          ['金额大写', this.form.word, this.form.word, 'word'],
          ['使用日期', this.form.use_time, this.form.use_time, 'use_time'],
          ['归还日期', this.form.return_time, this.form.return_time, 'return_time'],
          ['申请事由', this.form.reason, this.form.reason, 'reason'],
          ['备注', this.form.remark, this.form.remark, 'remark']
        ]))
        this.handleCreate(e)
      })
    }
  }
}
</script>

<style>
</style>
